package com.tanhua.server.interceptor;

import com.tanhua.common.pojo.User;
import com.tanhua.server.service.UserService;
import com.tanhua.server.utils.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by 景银泰
 * date 2020-11-14 19:44
 * Description :
 * Version :
 */
@Component
public class TokenInterceptor implements HandlerInterceptor {

    @Autowired
    private UserService userService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("拦截器！！！！！！！！！！！！！！！！！！！！！！！！！！！！！");
        System.out.println("拦截器！！！！！！！！！！！！！！！！！！！！！！！！！！！！！");
        System.out.println("拦截器！！！！！！！！！！！！！！！！！！！！！！！！！！！！！");
        System.out.println("拦截器！！！！！！！！！！！！！！！！！！！！！！！！！！！！！");
        System.out.println("拦截器！！！！！！！！！！！！！！！！！！！！！！！！！！！！！");
        String token = request.getHeader("Authorization");
        System.out.println(token);
        User user = this.userService.queryUserByToken(token);
        System.out.println(user);
        if (null == user) {
            response.setStatus(401);
            return false;
        }
        UserThreadLocal.set(user);
        return true;
    }
}
